package HashTab;

import java.util.Scanner;

public class HashTabDemo {

  public static void main(String[] args) {

//    创建hashtab
    HashTab hashTab = new HashTab(7);

//    写个简单的菜单
    String key = "";
    Scanner scanner = new Scanner(System.in);

    while (true){
      System.out.println("add:添加");
      System.out.println("list:查看");
      System.out.println("find:查找");
      System.out.println("exit:退出");

      key = scanner.next();

      switch (key) {
        case "add":
          System.out.println("输入id");
          int id = scanner.nextInt();
          System.out.println("输入姓名");
          String name = scanner.next();
          Emp emp = new Emp(id,name);
          hashTab.add(emp);
          break;
        case "list":
          hashTab.list();
          break;
        case "find":
          System.out.println("输入员工id");
          int empId = scanner.nextInt();
          hashTab.findByEmpId(empId);
          break;
        case "exit":
          scanner.close();
          System.exit(0);
          break;
        default:
          break;
      }
    }
  }
}

class Emp{
  public int id;
  public String name;
  public Emp next;

  public Emp(int id,String name){
    super();
    this.id = id;
    this.name = name;
  }
}
class EmpLinkedList{
  private Emp head;

//  添加雇员
  public void add(Emp emp){

//    当链表无数据时，直接添加
    if (head == null){
      head = emp;
      return;
    }
    Emp cur = head;
    while (true){
      if (cur.next == null){
        break;
      }
      cur = cur.next;
    }
    cur.next = emp;
  }

//  遍历雇员
  public void list(int id){
    if (head == null){
      System.out.println("链表" + id + "为空");
      return;
    }
    System.out.print("第" + id + "条链表的信息=》");
    Emp cur = head;
    while (true){
      System.out.print( "id = " + cur.id + ",name = " + cur.name);
      if (cur.next == null){
        break;
      }
      cur = cur.next;
    }
    System.out.println();
  }

//  根据id查找雇员
  public Emp findByEmpId(int id){
    if (head == null){
      return null;
    }
    Emp cur = head;
    while (true){

      if (cur.id == id){//表示已经找到
        break;
      }

      if (cur.next == null){
        cur = null;//表示查不到数据
        break;
      }

      cur = cur.next;
    }
    return cur;
  }
}

//创建HashTab
class HashTab{
  private EmpLinkedList[] empLinkedListArray;
  private int size;//表示有多少链表


  public HashTab(int size){
    this.size = size;
    empLinkedListArray = new EmpLinkedList[size];
//    不要忘了初始化每一条链表
    for (int i = 0; i < size; i++) {
      empLinkedListArray[i] = new EmpLinkedList();
    }
  }

//  添加雇员
  public void add(Emp emp){
    //根据员工的id ,得到该员工应当添加到哪条链表
    int empLinkedListNO = HashFun(emp.id);
    //将emp添加到对应的链表中
    empLinkedListArray[empLinkedListNO].add(emp);
  }

//  遍历所有的链表
  public void list(){
    for (int i = 0; i < size; i++) {
      empLinkedListArray[i].list(i);
    }
  }

//  根据id查找雇员
  public void findByEmpId(int id){
    int empID = HashFun(id);
    Emp emp = empLinkedListArray[empID].findByEmpId(id);
    if (emp != null){
      System.out.println(emp);
    }else {
      System.out.println("查无此人");
    }
  }


  //  散列函数
  public int HashFun(int id){
    return id % size;
  }

}